package com.yld.car.market;

import java.util.ArrayList;

import com.yld.car.application.MyApplication;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Shader.TileMode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;

public class PhotoLargeActivity extends BaseActivity implements
		OnItemSelectedListener {

	private ArrayList<Bitmap> allBitmaps = null;

	private TextView titleText;

	protected void onCreate(android.os.Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		MyApplication application = (MyApplication) getApplication();
		allBitmaps = application.getAllBitmaps();
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.car_full_screen_layout);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);

		titleText = ((TextView) findViewById(R.id.title));

		titleText.setText("1/" + allBitmaps.size());
		showButton();

		Gallery gallery = (Gallery) findViewById(R.id.gallery);

		gallery.setOnItemSelectedListener(this);
		ImageAdapter adapter = new ImageAdapter();
		gallery.setAdapter(adapter);
	}

	private void showButton() {

		Button localButton = (Button) findViewById(R.id.leftButton);
		localButton.setVisibility(View.VISIBLE);

		localButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				finish();

			}
		});
	}

	@Override
	public void onItemSelected(AdapterView<?> adapterView, View v,
			int position, long id) {

		titleText.setText((position + 1) + "/" + allBitmaps.size());
	}

	@Override
	public void onNothingSelected(AdapterView<?> adapterView) {

	}

	private class ImageAdapter extends BaseAdapter {

		@Override
		public int getCount() {

			return allBitmaps.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return allBitmaps.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			if (convertView == null) {
				convertView = LayoutInflater.from(getApplicationContext())
						.inflate(R.layout.car_large_image_layout_items, null);
			}
			ImageView imageView = (ImageView) convertView
					.findViewById(R.id.pic);

			imageView.setImageBitmap(allBitmaps.get(position));
			return convertView;
		}

	}
}
